Algorithm
Problem Name: Python -
In this HackerRank Functions in PYTHON problem solution,
start() & end()
These expressions return the indices of the start and end of the substring matched by the group.
Code
>>> import re
>>> m = re.search(r'\d+','1234')
>>> m.end()
4
>>> m.start()
0
Task
You are given a string S.
Your task is to find the indices of the start and end of string k in S.
Input Format
The first line contains the string S.
The second line contains the string k.
Constraints
0 < len(S) < 100
0 < len(S) < len(k)
Output Format
Print the tuple in this format: (start _index, end _index).
If no match is found, print (-1, -1)
.
Sample Input
aaadaa
aa
Sample Output
(0, 1)
(1, 2)
(4, 5)
Code Examples
#1 Code Example with Python Programming
Code -
Python Programming
import re
string = input().strip()
match = input().strip()
List_index = []
for i in range(0,len(string)):
search = re.search(match,string[i:])
if search and not (search.start()+i,search.end()-1+i) in List_index:
print((search.start()+i,search.end()-1+i))
List_index.append((search.start()+i,search.end()-1+i))
else:
if search and (len(string)==search.end()+i):
break
if not List_index:
print((-1,-1))
Copy The Code &
Try With Live Editor